package com.kobe.game_20;

/**
 * 
 * If the numbers 1 to 5 are written out in words: one, two, three, four, five,
 * then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
 * 
 * If all the numbers from 1 to 1000 (one thousand) inclusive were written out
 * in words, how many letters would be used?
 * 
 * 
 * 
 * NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and
 * forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20
 * letters. The use of "and" when writing out numbers is in compliance with
 * British usage.
 * 
 * 
 */
public class Game17 {
    public static void main(String[] args) {
        int result = 0;
        for (int i = 1; i <= 1000; i++) {
            result += getWordsLength(i);
        }
        System.out.println("the result is: " + result);
    }

    private static int getWordsLength(int number) {
        int result = 0;
        if (number < 20) {
            result = getLength1_19(number);
        } else if (number >= 20 && number < 100) {
            result = getLength20_99(number);
        } else if (number >= 100 && number < 1000) {
            result = getLength100_999(number);
        } else if (number == 1000) {
            result = 3 + 8;
        }

        return result;
    }

    private static int getLength100_999(int number) {
        int result = 0;
        if (number % 100 == 0) {
            String tempNumber = String.valueOf(number);
            int highestBit = Integer.valueOf(tempNumber.substring(0, 1));
            result = getLength1_19(highestBit) + 7;
        } else {
            String tempNumber = String.valueOf(number);
            int highestBit = Integer.valueOf(tempNumber.substring(0, 1));
            int lowerBits = Integer.valueOf(tempNumber.substring(1, tempNumber
                    .length()));
            result = getLength1_19(highestBit) + 7 + 3
                    + getLength1_99(lowerBits);
        }

        return result;
    }

    private static int getLength1_99(int number) {
        if (number < 20)
            return getLength1_19(number);
        else
            return getLength20_99(number);
    }

    private static int getLength20_99(int number) {
        int returnValue = 0;
        if ((number >= 20 && number < 40) || (number >= 80 && number < 100)) {
            returnValue = 6;
        }
        if (number >= 40 && number < 70) {
            returnValue = 5;
        }
        if (number >= 70 && number < 80) {
            returnValue = 7;
        }
        String tempValue = String.valueOf(number);
        int lowestBit = Integer.valueOf(tempValue.substring(
                tempValue.length() - 1, tempValue.length()));
        return returnValue + getLength1_19(lowestBit);
    }

    private static int getLength1_19(int number) {
        int returnValue = 0;
        if (number == 1 || number == 2 || number == 6 || number == 10)
            returnValue = 3;
        if (number == 3 || number == 7 || number == 8)
            returnValue = 5;
        if (number == 4 || number == 5 || number == 9)
            returnValue = 4;
        if (number == 11 || number == 12 || number == 20)
            returnValue = 6;
        if (number == 13 || number == 14 || number == 18 || number == 19)
            returnValue = 8;
        if (number == 15 || number == 16)
            returnValue = 7;
        if (number == 17)
            returnValue = 9;
        return returnValue;
    }

}
